﻿using System;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections;
using Internet_Cafe.App_Code;
using Internet_Cafe.App_Code.DatabaseModel;
using System.Collections.Generic;

namespace Internet_Cafe
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<Computer> lstComputers;
        public List<User> userList;
        public List<App_Code.DatabaseModel.Bill> lstBills;
        public List<App_Code.DatabaseModel.Bill> lstIzvestai;
        public User currentlyLogged;
        public MainWindow()
        {
            InitializeComponent();
            if (AppHelper.LoggedUser.isAdmin)
            {

            }
            else
            {
                btnCreate.IsEnabled = false;
                cmbUserBox.IsEnabled = false;
                cmbUserBoxBill.IsEnabled = false;
                cmbType.IsEnabled = false;
                btnDelete.IsEnabled = false;
                txtEmail.IsEnabled = false;
                txtUserName.IsEnabled = false;
                txtFirstName.IsEnabled = false;
                txtLastName.IsEnabled = false;
                txtPassword.IsEnabled = false;
                tabIzvestaj.Visibility = System.Windows.Visibility.Hidden;
            }
            lstComputers = App_Code.DataBaseAccess.DbComputersAccess.GetComputers();
            User temp = null;
            userList = App_Code.DataBaseAccess.DbUsersAccess.GetUsers();
            foreach (User u in userList)
            {
                if (u.ID == AppHelper.LoggedUser.user.ID)
                {
                    temp = u;
                    break;
                }

            }
            fillCanvas();
            txtTitleFirstName.Text = AppHelper.LoggedUser.user.FirstName;
            txtTitleLastName.Text = AppHelper.LoggedUser.user.LastName;
            txtTitleType.Text = AppHelper.LoggedUser.isAdmin == true ? "Администратор" : "Вработен";
            cmbUserBox.ItemsSource = userList;
            cmbUserBox.SelectedIndex = userList.IndexOf(temp);
            cmbUserBoxBill.ItemsSource = userList;
            cmbUserBoxBill.SelectedIndex = userList.IndexOf(temp);
            
            txtCurrentlyLoggedUser.Text = AppHelper.LoggedUser.user.Username + " ( " + (AppHelper.LoggedUser.isAdmin == true ? "Администратор" : "Вработен") + " )";
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            AddNewPC newWindow = new AddNewPC(this);
            newWindow.ShowDialog();
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        // Zakluci gi site elementi vo dragCanvasot
        public void lockItems()
        {
            IEnumerator enumerator = canvasComputers.Children.GetEnumerator();
            while (enumerator.MoveNext())
            {
                DragCanvas.SetCanBeDragged((ComputerItem)enumerator.Current, false);
                foreach (Computer c in lstComputers)
                {
                    if (c.IPAddress == ((ComputerItem)enumerator.Current).txtIP.Text)
                    {
                        UpdateCoordinates(c, (ComputerItem)enumerator.Current);
                        break;
                    }
                }

            }
        }

        // Odkluci gi site elementi vo dragCanvasot
        public void unlockItems()
        {
            IEnumerator enumerator = canvasComputers.Children.GetEnumerator();
            while (enumerator.MoveNext())
            {
                DragCanvas.SetCanBeDragged((ComputerItem)enumerator.Current, true);
                foreach (Computer c in lstComputers)
                {
                    if (c.IPAddress == ((ComputerItem)enumerator.Current).txtIP.Text)
                    {
                        UpdateCoordinates(c, (ComputerItem)enumerator.Current);
                        break;
                    }
                }
            }
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            MenuItem item = (MenuItem)sender;
            if (item.IsChecked == false)
            {
                lockItems();
            }
            else if (item.IsChecked == true)
            {
                unlockItems();
            }
        }

        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Оваа семинарска е издработена од:\n Бојан Велјановски 943/2009 \n Филип Марковски 912/2009", "За нас!", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            EditUser eu = new EditUser((User)cmbUserBox.SelectedItem);
            eu.ShowDialog();
            txtTitleFirstName.Text = AppHelper.LoggedUser.user.FirstName;
            txtTitleLastName.Text = AppHelper.LoggedUser.user.LastName;
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            User newUser = new User();
            if (txtUserName.Text.Length <= 0)
            {
                MessageBox.Show("Полето „Корисничко име“ не смее да биде празно!", "Празно корисничко име!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            if (txtPassword.Password.Length <= 0)
            {
                MessageBox.Show("Полето „Лозинка“ не смее да биде празно!", "Празна лозинка!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            if (cmbType.SelectedIndex == -1)
            {
                MessageBox.Show("Изберете го типот на корисник!", "Непостоечки тип на корисник!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }

            newUser.FirstName = txtFirstName.Text;
            newUser.LastName = txtLastName.Text;
            newUser.Username = txtUserName.Text;
            newUser.Password = txtPassword.Password;
            newUser.Email = txtEmail.Text;
            App_Code.DataBaseAccess.DbUsersAccess.Insert(newUser, cmbType.SelectedIndex == 0 ? true : false);
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtUserName.Text = "";
            txtPassword.Password = "";
            txtEmail.Text = "";
            cmbType.SelectedIndex = -1;
            MessageBox.Show("Успешно е креиран корисник", "Успешно!", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (AppHelper.LoggedUser.user == (User)cmbUserBox.SelectedItem)
            {
                MessageBox.Show("Неможе да се избрише активниот корисник!", "Грешка", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            App_Code.DataBaseAccess.DbUsersAccess.Delete(((User)cmbUserBox.SelectedItem).ID);
            MessageBox.Show("Успешно избришан корисник!", "Успешна акција", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void MenuItem_Click_4(object sender, RoutedEventArgs e)
        {
            AppHelper.LoggedUser.user = null;
            Login login = new Login();
            this.Close();
            login.Show();
        }

        private void UpdateCoordinates(Computer c, ComputerItem ci)
        {
            double left = DragCanvas.GetLeft(ci);
            double top = DragCanvas.GetTop(ci);
            App_Code.DataBaseAccess.DbComputersAccess.SetComputerPosition(c.ID, left, top);
        }

        public void updateComputers()
        {
            IEnumerator enumerator = canvasComputers.Children.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Computer temp = null;
                foreach (Computer c in lstComputers)
                {
                    if (c.ID == ((ComputerItem)enumerator.Current).ID)
                    {
                        temp = c;
                        break;
                    }

                }
                App_Code.DataBaseAccess.DbComputersAccess.Update(((ComputerItem)enumerator.Current).ID, temp);
                foreach (Computer c in lstComputers)
                {
                    if (c.IPAddress == ((ComputerItem)enumerator.Current).txtIP.Text)
                    {
                        UpdateCoordinates(c, (ComputerItem)enumerator.Current);
                        break;
                    }
                }
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            lstBills = App_Code.DataBaseAccess.DbAccountingAccess.GetBills(((User)cmbUserBoxBill.SelectedItem).ID);
            lvSmetki.ItemsSource = lstBills;
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            lstIzvestai = App_Code.DataBaseAccess.DbAccountingAccess.GetBills();
            lvIzvestai.ItemsSource = lstIzvestai;
        }

        public void fillCanvas()
        {
            canvasComputers.Children.Clear();
            foreach (Computer c in lstComputers)
            {
                ComputerItem ci = new ComputerItem(c.IPAddress, c.ID);
                canvasComputers.Children.Add(ci);
                DragCanvas.SetLeft(ci, (double)c.PosLeft);
                DragCanvas.SetTop(ci, (double)c.PosTop);
            }
            lockItems();
        }

        private void TabItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            fillCanvas();
        }
    }
}
